
************************************************************************************************
************************************************************************************************
*This .do file creates the results relating to the RD design for long-run outcomes

*Inputs:
*1. "student_mb_2009to2017.dta" (created by "Data_get_2009_17.do")
*2. "/data_analysis/Value Added Generate/output/sat_2009_2019.dta"
*3. "/data_analysis/NC_RD_Retake/long_run_grad_data.dta"
*4. "/data_analysis/Value Added Generate/output/psat_2013_2019.dta"

*Results created by .do file:
*1. Table 3
************************************************************************************************
************************************************************************************************
clear all
use "/data_analysis/NC_RD_Retake/student_mb_2009to2017.dta"

*No need for post-2012 data
drop if year>2012
*Drop grade 8
drop if grade==8

*Drop the extend tests (disabled tests)
replace smathscal=. if nc_extend_math==1
replace sreadscal=. if nc_extend_read==1
drop nc_extend_math nc_extend_math_retake nc_extend_read nc_extend_read_retake
drop lag_smathscal lag_sreadscal

*Need current or subsequent scores in at least one subject to be in RD
drop if (smathscal==. | Fsmathscal==.) & (sreadscal==. | Fsreadscal==.)

*Create treatment indicator
gen treat=(running_math<=0)
qui compress
*Missing read indicator
gen read_missing=(read==.)
foreach g of numlist 3(1)7 {
qui su read if grade==`g'
replace read=r(mean) if read_missing==1 & grade==`g'
}

*Set repeat next year to zero if missing
replace repeat_next_year=0 if repeat_next_year==.
replace repeating=0 if repeating==.
local demo_controls "i.sex i.lep i.swd i.ethnic i.eds i.aig_math i.aig_read i.repeating"
local test_controls_math "i.grade#c.read i.grade#c.math i.read_missing i.read_missing#i.grade#c.math"

*Math sample restrictions
*Need a current same subject score
drop if smathscal==.
*Need a subsequent same subject score
drop if Fsmathscal==.

*Create dummy for reghdfe command
gen dum=1

**************************************************************
*SAT and PSAT and HS Grad Regressions
**************************************************************
egen gradeyear=group(grade year)

*Need to be in grade 11 by 2019 for the PSAT AND grade 10 by 2013.  This means you must be in grade 5 by 2012 and also drops grade 7 in 2009
merge m:1 mastid using "/data_analysis/Value Added Generate/output/psat_2013_2019.dta"
drop if _merge==2
drop _merge
gen took_psat=(psat_score!=.)
*Grade 11 by 2019
replace took_psat=. if year==2012 & grade==3
replace psat_score=. if year==2012 & grade==3
*Grade 10 by 2013.
replace took_psat=. if year==2009 & grade==7
replace psat_score=. if year==2009 & grade==7


*Need to be in grade 12 by 2019 for the SAT.  This means you must be in grade 5 by 2012
merge m:1 mastid using "/data_analysis/Value Added Generate/output/sat_2009_2019.dta"
drop if _merge==2
drop _merge
gen took_sat=(sat_score!=.)
replace took_sat=. if year==2012 & (grade==3 | grade==4)
replace took_sat=. if year==2011 & grade==3
replace sat_score=. if year==2012 & (grade==3 | grade==4)
replace sat_score=. if year==2011 & grade==3

*Need to be in grade 12 by 2019 for HS Graduation (giving 1 extra year).  This means you must be in grade 6 by 2012
merge m:1 mastid using "/data_analysis/NC_RD_Retake/long_run_grad_data.dta"
drop if _merge==2
drop _merge
replace grad=0 if grad==.
replace grad=. if year==2012 & (grade==3 | grade==4 | grade==5)
replace grad=. if year==2011 & (grade==3 | grade==4)
replace grad=. if year==2010 & (grade==3)

*PSAT 
reghdfe took_psat treat running_math c.running_math#i.treat `test_controls_math' `demo_controls' if running_math>=-5 & running_math<=5, absorb(gradeyear) cluster(schoolid mastid)
reghdfe psat_score treat running_math c.running_math#i.treat `test_controls_math' `demo_controls' if running_math>=-5 & running_math<=5, absorb(gradeyear) cluster(schoolid mastid)
*SAT
reghdfe took_sat treat running_math c.running_math#i.treat `test_controls_math' `demo_controls' if running_math>=-5 & running_math<=5, absorb(gradeyear) cluster(schoolid mastid)
reghdfe sat_score treat running_math c.running_math#i.treat `test_controls_math' `demo_controls' if running_math>=-5 & running_math<=5, absorb(gradeyear) cluster(schoolid mastid)
*HS Graduation
reghdfe grad treat running_math c.running_math#i.treat `test_controls_math' `demo_controls' if running_math>=-5 & running_math<=5, absorb(gradeyear) cluster(schoolid mastid)

*Save to stack math and English
keep grad took_psat psat_score took_sat sat_score treat running_math sex lep swd ethnic eds aig_math aig_read repeating grade year read math read_missing schoolid mastid

ren running_math running
ren read other_sub
ren math own_sub
ren read_missing other_sub_missing

save "/data_analysis/NC_RD_Retake/super_crazy_temp_math.dta", replace



***********************************************************************************
***********************************************************************************
*****************REDOES ANALYSIS FOR ENGLISH THRESHOLD (Panel B)*******************
***********************************************************************************
***********************************************************************************
clear all
use "/data_analysis/NC_RD_Retake/student_mb_2009to2017.dta"

*No need for post-2012 data
drop if year>2012
*Drop grade 8
drop if grade==8

*Deal with extends
replace smathscal=. if nc_extend_math==1
replace sreadscal=. if nc_extend_read==1
drop nc_extend_math nc_extend_math_retake nc_extend_read nc_extend_read_retake
drop lag_smathscal lag_sreadscal

*Need current or subsequent scores in at least one subject
drop if (smathscal==. | Fsmathscal==.) & (sreadscal==. | Fsreadscal==.)

*Create treatmen indicator
gen treat=(running_read<=0)
qui compress
*Missing math indicator
gen math_missing=(math==.)
foreach g of numlist 3(1)7 {
qui su math if grade==`g'
replace math=r(mean) if math_missing==1 & grade==`g'
}

*Set repeat next year to zero if missing
replace repeat_next_year=0 if repeat_next_year==.
replace repeating=0 if repeating==.
local demo_controls "i.sex i.lep i.swd i.ethnic i.eds i.aig_math i.aig_read i.repeating"
local test_controls_read "i.grade#c.math i.grade#c.read i.math_missing i.math_missing#i.grade#c.read"

*Math sample restrictions
*Need a current same subject score
drop if sreadscal==.
*Need a subsequent same subject score
drop if Fsreadscal==.

*Create dummy for reghdfe command
gen dum=1

**************************************************************
*SAT and PSAT and HS Grad Regressions
**************************************************************
egen gradeyear=group(grade year)

*Need to be in grade 11 by 2019 for the PSAT AND grade 10 by 2013.  This means you must be in grade 5 by 2012 and also drops grade 7 in 2009
merge m:1 mastid using "/data_analysis/Value Added Generate/output/psat_2013_2019.dta"
drop if _merge==2
drop _merge
gen took_psat=(psat_score!=.)
*Grade 11 by 2019
replace took_psat=. if year==2012 & grade==3
replace psat_score=. if year==2012 & grade==3
*Grade 10 by 2013.
replace took_psat=. if year==2009 & grade==7
replace psat_score=. if year==2009 & grade==7


*Need to be in grade 12 by 2019 for the SAT.  This means you must be in grade 5 by 2012
merge m:1 mastid using "/data_analysis/Value Added Generate/output/sat_2009_2019.dta"
drop if _merge==2
drop _merge
gen took_sat=(sat_score!=.)
replace took_sat=. if year==2012 & (grade==3 | grade==4)
replace took_sat=. if year==2011 & grade==3
replace sat_score=. if year==2012 & (grade==3 | grade==4)
replace sat_score=. if year==2011 & grade==3

*Need to be in grade 12 by 2019 for HS Graduation (giving 1 extra year).  This means you must be in grade 6 by 2012
merge m:1 mastid using "/data_analysis/NC_RD_Retake/long_run_grad_data.dta"
drop if _merge==2
drop _merge
replace grad=0 if grad==.
replace grad=. if year==2012 & (grade==3 | grade==4 | grade==5)
replace grad=. if year==2011 & (grade==3 | grade==4)
replace grad=. if year==2010 & (grade==3)

*PSAT 
reghdfe took_psat treat running_read c.running_read#i.treat `test_controls_read' `demo_controls' if running_read>=-5 & running_read<=5, absorb(gradeyear) cluster(schoolid mastid) 
reghdfe psat_score treat running_read c.running_read#i.treat `test_controls_read' `demo_controls' if running_read>=-5 & running_read<=5, absorb(gradeyear) cluster(schoolid mastid) 
*SAT
reghdfe took_sat treat running_read c.running_read#i.treat `test_controls_read' `demo_controls' if running_read>=-5 & running_read<=5, absorb(gradeyear) cluster(schoolid mastid) 
reghdfe sat_score treat running_read c.running_read#i.treat `test_controls_read' `demo_controls' if running_read>=-5 & running_read<=5, absorb(gradeyear) cluster(schoolid mastid) 
*HS Graduation
reghdfe grad treat running_read c.running_read#i.treat `test_controls_read' `demo_controls' if running_read>=-5 & running_read<=5, absorb(gradeyear) cluster(schoolid mastid) 

*Save to stack math and English
keep grad took_psat psat_score took_sat sat_score treat running_read sex lep swd ethnic eds aig_math aig_read repeating grade year read math math_missing schoolid mastid
ren running_read running
ren math other_sub
ren read own_sub
ren math_missing other_sub_missing
save "/data_analysis/NC_RD_Retake/super_crazy_temp_read.dta", replace


***********************************************************************************
***********************************************************************************
*****************REDOES ANALYSIS FOR STACKED ANALYSIS (Panel C)********************
***********************************************************************************
***********************************************************************************
clear all
use "/data_analysis/NC_RD_Retake/super_crazy_temp_math.dta"
gen math=1
append using "/data_analysis/NC_RD_Retake/super_crazy_temp_read.dta"
replace math=0 if math==.

local demo_controls "i.sex i.lep i.swd i.ethnic i.eds i.aig_math i.aig_read i.repeating"
local test_controls "i.grade#c.other_sub i.grade#c.own_sub i.other_sub_missing i.other_sub_missing#i.grade#c.own_sub"

egen gradeyear=group(grade year)
*PSAT 
reghdfe took_psat treat running c.running#i.treat i.math `test_controls' `demo_controls' if running>=-5 & running<=5, absorb(gradeyear) cluster(schoolid mastid) 
reghdfe psat_score treat running c.running#i.treat i.math `test_controls' `demo_controls' if running>=-5 & running<=5, absorb(gradeyear) cluster(schoolid mastid) 
*SAT
reghdfe took_sat treat running c.running#i.treat i.math `test_controls' `demo_controls' if running>=-5 & running<=5, absorb(gradeyear) cluster(schoolid mastid) 
reghdfe sat_score treat running c.running#i.treat i.math `test_controls' `demo_controls' if running>=-5 & running<=5, absorb(gradeyear) cluster(schoolid mastid) 
*HS Graduation
reghdfe grad treat running c.running#i.treat i.math `test_controls' `demo_controls' if running>=-5 & running<=5, absorb(gradeyear) cluster(schoolid mastid) 

erase "/data_analysis/NC_RD_Retake/super_crazy_temp_read.dta"
erase "/data_analysis/NC_RD_Retake/super_crazy_temp_math.dta"





